package com.shundr.auth.common.view;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.shundr.auth.R;
import com.shundr.auth.base.BaseDialog;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.frame.util.ToastHelper;

/**
 * 
 * DialogAuth
 * 
 * @author larry 2015-4-16 下午7:04:38
 * @version 1.0.0
 *
 */
public class DialogAuthConfirm extends BaseDialog {

	private Context mContext;
	private EditText etReason;

	private ConfirmedListener confirmedListener;

	public DialogAuthConfirm(Context context) {
		this.mContext = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature((int) Window.FEATURE_NO_TITLE);
		View view = inflater.inflate(R.layout.dialog_auth_confirm, null);
		etReason = (EditText) view.findViewById(R.id.et_reason);
		Button btnReject = (Button) view.findViewById(R.id.btn_reject);
		Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
		btnReject.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (confirmedListener != null) {
					if (StringUtil.isNull(etReason.getText().toString())) {
						ToastHelper.toastShort(mContext, "请输入驳回原因");
						return;
					}
					confirmedListener.onReject(etReason.getText().toString());
				}
				dismiss();
			}
		});
		btnConfirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (confirmedListener != null) {
					String reason = "审核通过";
					if (!StringUtil.isNull(etReason.getText().toString())) {
						reason = etReason.getText().toString();
					}
					confirmedListener.onConfirm(reason);
				}
				dismiss();
			}
		});

		Window dialogWindow = this.getDialog().getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);

		/*
		 * lp.x与lp.y表示相对于原始位置的偏移.
		 * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
		 * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
		 * 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
		 * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
		 * 当参数值包含Gravity.CENTER_HORIZONTAL时
		 * ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
		 * 当参数值包含Gravity.CENTER_VERTICAL时
		 * ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
		 * gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
		 * Gravity.CENTER_VERTICAL.
		 * 
		 * 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
		 * 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了, Gravity.LEFT, Gravity.TOP,
		 * Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
		 */
		lp.width = 500; // 宽度
		lp.height = 300; // 高度
		dialogWindow.setAttributes(lp);
		return view;
	}

	public void setConfirmedListener(ConfirmedListener confirmedListener) {
		this.confirmedListener = confirmedListener;
	}

	public interface ConfirmedListener {
		public void onReject(String reason);

		public void onConfirm(String reason);
	}

}
